package com.zhiyume.controller;

import com.zhiyume.api.util.ApiClassifyEnum;
import com.zhiyume.dao.api.TCmsRecommendMapper;
import com.zhiyume.dao.entity.*;
import com.zhiyume.dao.entity.TCmsRecommend;
import com.zhiyume.dao.entity.TCmsRecommend;
import com.zhiyume.service.CmsRecommendService;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.service.util.GenerateKeyUtil;
import com.zhiyume.shiro.AdminSecurityManager;
import com.zhiyume.util.TypeConstants;
import com.zhiyume.vo.CommonReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wang on 18/3/6.
 */
@Controller
@RequestMapping("/cmsRecommend")
public class CmsRecommendController{

    @Autowired
    private TCmsRecommendMapper tCmsRecommendMapper;

    @Autowired
    private CmsRecommendService cmsRecommendService;

    /**
     * 获取所有分类的资讯列表
     * @param commonReqVO
     * @return
     */
    @RequestMapping(value="/getCmsRecommendList")
    @ResponseBody
    public Object getCmsRecommendList(CommonReqVO commonReqVO){
        List<TCmsRecommend> cms = cmsRecommendService.getCmsRecommendList(commonReqVO);
        int count = cmsRecommendService.getCmsRecommendCount(commonReqVO);
        Map result = new HashMap();
        result.put("list",cms);
        result.put("total",count);
        result.put("page",commonReqVO.getPage());
        return result;
    }

    @RequestMapping("/addCmsRecommend")
    @ResponseBody
    public Map addCmsRecommend(TCmsRecommend cmsRecommend){
        return addRecommend(cmsRecommend);
    }


    private Map addRecommend(TCmsRecommend cmsRecommend){
        String result = "success";
        TAdminSys sysUser = AdminSecurityManager.getSessionTAdminSys();

        cmsRecommend.setCreateId(sysUser.getId());
        cmsRecommend.setUpdateId(sysUser.getId());
        cmsRecommend.setCreateTime(DateUtil.getNow());
        cmsRecommend.setUpdateTime(DateUtil.getNow());
        try {
            cmsRecommend.setId(GenerateKeyUtil.generationPK());
            cmsRecommend.setStatus(TypeConstants.COMMON_INVALID);
            tCmsRecommendMapper.insert(cmsRecommend);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }

    @RequestMapping("/cmsRecommendListPage")
    public ModelAndView CmsHomeListPage(){
        ModelAndView modelAndView = new ModelAndView();
        List navList = ApiClassifyEnum.getNavList();
        modelAndView.addObject("navList", navList);
        modelAndView.setViewName("page/cmsRecommend/cmsRecommendList");
        return modelAndView;
    }

    @RequestMapping("/cmsRecommendAddPage")
    public ModelAndView CmsRecommendAddPage(){
        ModelAndView modelAndView = new ModelAndView();
        // 栏目下拉列表
        List navList = ApiClassifyEnum.getNavList();

        // 楼层下拉列表
        modelAndView.addObject("navList", navList);

        modelAndView.addObject("cmsRecommend",new TCmsRecommend());
        modelAndView.setViewName("page/cmsRecommend/cmsRecommendAdd");
        return modelAndView;
    }


    @RequestMapping("/validCmsRecommend")
    @ResponseBody
    public Map validCmsRecommend(Long id){
        String result = "success";
        TAdminSys sysUser = AdminSecurityManager.getSessionTAdminSys();
        boolean isAdmin = AdminSecurityManager.isSessionUserAdminRole();

        TCmsRecommend cmsRecommend = new TCmsRecommend();
        try {
            if(!isAdmin) throw new Exception("【警告，非权限者操作侵入】");
            cmsRecommend = cmsRecommendService.findCmsRecommend(id);

            cmsRecommend.setUpdateId(sysUser.getId());
            cmsRecommendService.validCmsRecommend(cmsRecommend);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }

    @RequestMapping("/inValidCmsRecommend")
    @ResponseBody
    public Map inValidCmsRecommend(Long id){
        String result = "success";
        TAdminSys sysUser = AdminSecurityManager.getSessionTAdminSys();
        boolean isAdmin = AdminSecurityManager.isSessionUserAdminRole();

        TCmsRecommend cmsRecommend = new TCmsRecommend();
        try {
            if(!isAdmin) throw new Exception("【警告，非权限者操作侵入】");

            cmsRecommend = cmsRecommendService.findCmsRecommend(id);
            cmsRecommend.setUpdateId(sysUser.getId());
            cmsRecommendService.inValidCmsRecommend(cmsRecommend);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }
    
  
}
